package admin.web.beans;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import common.MessageCallback;

import authentication.ejb.usermanagement.UserMgr;
import user.ejb.eaos.UserList;
import dtos.UserDTO;


@SuppressWarnings("serial")
@ManagedBean(name="userListBean")
@ViewScoped
public class UserListBean implements Serializable {  
    
    /**
	 * 
	 */

	private final static Logger logger = Logger.getLogger(UserListBean.class.getName());  
     
    @EJB
	private UserMgr userMgr;
    @EJB
    private UserList userList;     
    private UserDTO selectedUser; 
	private DataModel<UserDTO> _model;
	private DataModel<UserDTO> _modelE;
    

    @PostConstruct
	public void refresh()
	{
    	List<UserDTO> c = new LinkedList<>();
		for(UserDTO u : userList.getUserList())
			c.add(u);
		_model = new ListDataModel<>(c);
		
		List<UserDTO> c2 = new LinkedList<>();
		for(UserDTO u : userList.getEmployeeList())
			c2.add(u);
		_modelE = new ListDataModel<>(c2);
	}
	
      
    private void create(){
    	userList.add(selectedUser);
    }
    
    public UserDTO getSelectedUser() {  
        return selectedUser;  
    }  
  
    public void setSelectedUser(UserDTO selectedUser) {  
        this.selectedUser = selectedUser;  
    }  
    

	public DataModel<UserDTO> getModel()
	{

		return _model;
	
	}
	
	public DataModel<UserDTO> getEmployees(){
		return _modelE;
	}

	public void addEmployee(UserDTO u){
		
		userMgr.addRole(u.getUsername(), new MessageCallback(FacesContext.getCurrentInstance()));
	}



}
